<?php
namespace Cyfy\Modules\Articles\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\Articles\Module as Articles;
use \Cyfy\Modules\Image\Image;
use \Cyfy\Modules\Locale\Locale;
use \Cyfy\Modules\BasicContent\Content;

class NewSection extends Forms\Form
{
	function structure()
	{
        $this -> permission("Articles", "administration");

		$i = new Forms\TextBox("title", self :: t(10));
		$i -> required = true;
		$i -> maxlength = 255;
		$this -> addItem($i);
		
        $i = new Forms\TextBox("path", self :: t(20));
		$i -> required = true;
        $i -> format = "/^[0-9A-Za-z-]+$/";
		$i -> maxlength = 255;
		$this -> addItem($i);
		
		$b = new Forms\Button(self :: t(30));
		$this -> addButton($b);		
    }
  
    function validate()
    {
        if(!\Cyfy\Cyfy :: tryPath($this -> get("path")))
        {
            \Cyfy\Message :: set(self :: t(51, array($this -> get("path"))), \Cyfy\Message::ERROR);
            return false;
        }
        return true;
    }
    
    
	function submit()
	{  
        $v = array(
            'title' => $this -> get("title"),
            'path' => $this -> get("path")
        );
            
        \dibi :: query("INSERT INTO [:cyfy:Articles-sections] %v", $v);
        
        \Cyfy\Cyfy :: registerPath($this -> get("path"), "Articles", \dibi :: insertId(), $this -> get("title"));
        
        $this -> onSubmit = "Cyfy.ajax('Articles', 'sectionsAdmin', '#sections-admin');";
        
        // show message
        Message :: set(self :: t(41, array($this -> get("title"))));
        return true;
	}
}